#include "../../server/mq_exchange.hpp"
#include <iostream>
#include <gtest/gtest.h>

using namespace xzt;

ExchangeManager::ptr p_em;

class ExchangeTest: public testing::Environment
{
    virtual void SetUp() override
    {
        p_em = std::make_shared<ExchangeManager>("./data/test.db");
    }

    virtual void TearDown() override
    {
        p_em->clear();
    }
};


TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};

    p_em->declareExchange("exchange1", ExchangeType::DIRECT, true, false, map);
    p_em->declareExchange("exchange2", ExchangeType::DIRECT, true, false, map);
    p_em->declareExchange("exchange3", ExchangeType::DIRECT, true, false, map);
    p_em->declareExchange("exchange4", ExchangeType::DIRECT, true, false, map);

    ASSERT_EQ(p_em->size(), 4);
}

TEST(exchange_test, select_test)
{
    std::string name = "exchange3";
    Exchange::ptr p_ex = p_em->selectExchange(name);

    ASSERT_NE(p_ex.get(), nullptr);
    ASSERT_EQ(p_ex->_name, name);
    ASSERT_EQ(p_ex->_type, ExchangeType::DIRECT);
    ASSERT_EQ(p_ex->_durable, true);
    ASSERT_EQ(p_ex->_auto_delete, false);
    ASSERT_EQ(p_ex->getArgs(), std::string("k1=v1&k2=v2"));
}


int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();

    return 0;
}


